package com.byxy.service.impl;

import com.byxy.mapper.UserMapper;
import com.byxy.po.User;
import com.byxy.service.UserService;
import com.byxy.util.RandomNum;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService<User> {


    @Resource
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public int deleteByPrimaryKey(User key) {
        return userMapper.deleteByPrimaryKey(key);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectOne(User record) {
        return userMapper.selectOne(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public void sendSms(String mobile) {
        //生成6位数验证码
        String checkcode = RandomNum.vcode(6);
        //验证码存入缓存,为了注册的时候校验验证码是否正确
        redisTemplate.opsForValue().set("checkcode"+mobile,checkcode,5, TimeUnit.MINUTES);
        //验证码放入消息队列
        Map<String,String> map = new HashMap();
        map.put("mobile",mobile);
        map.put("checkcode",checkcode);
        rabbitTemplate.convertAndSend("sms",map);

    }

}
